package elitech.vietnam.weatherfashion.backgroundtask;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.custom.dialogues.TimeOutDialog;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;

public class ButtonAskFriendTask extends AsyncTask<Integer, Integer, Boolean> {

	private ImageView button;
	private ProgressBar progress;
	private Activity activity;

	public ButtonAskFriendTask(Activity activity, ImageView button, ProgressBar progress) {
		super();
		this.button = button;
		this.progress = progress;
		this.activity = activity;
	}

	@Override
	protected Boolean doInBackground(Integer... params) {
		if (params == null)
			return false;
		if (params.length < 2)
			return false;
		PHPSoapServiceCaller appService = new PHPSoapServiceCaller();
		boolean status;
		try {
			status = appService.addFriend(params[0], params[1]);
		} catch (IOException e) {
			return null;
		}
		if (!status)
			return false;
		MemberDTO user = GlobalValue.getInstance(activity).readUserInfo();
		int memberId = user == null ? -1 : user.getId();

		try {
			WFApplication.getInstance().userFriends = appService.getListFriend(memberId);
		} catch (IOException e) {
			return null;
		}
		if (user != null)
			try {
				WFApplication.getInstance().stylerFriendData = appService.getFashionDayFriendLikeCountry(memberId, 0, Const.COUNTRY_CODE);
			} catch (IOException e) {
				return null;
			}
		else
			try {
				WFApplication.getInstance().stylerFriendData = appService.getFashionDayFriendLikeCountry(memberId, 0, Const.COUNTRY_CODE);
			} catch (IOException e) {
				return null;
			}
		return true;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		button.setEnabled(false);
		button.setVisibility(View.INVISIBLE);
		progress.setVisibility(View.VISIBLE);
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		if (result == null) {
			button.setEnabled(true);
			button.setVisibility(View.VISIBLE);
			progress.setVisibility(View.INVISIBLE);
			new TimeOutDialog(activity).show();
			return;
		}
		if (result) {
			AlertDialog.Builder bulder = new AlertDialog.Builder(activity);
			bulder.setTitle(activity.getString(R.string.confirm_exit)).setMessage(activity.getString(R.string.add_friend_success)).setPositiveButton(activity.getString(R.string.close), null).create()
					.show();
			button.setImageResource(R.drawable.icon_friended);
			if (WFApplication.getInstance().stylerFriendData != null)
				WFApplication.getInstance().stylerFriendData.clear();
		}
		button.setVisibility(View.VISIBLE);
		button.setOnClickListener(null);
		progress.setVisibility(View.GONE);

		button = null;
		progress = null;
		activity = null;
	}
}
